#include <stdlib.h>
#include <Def.h>

#include <T3d/T3dLoadImg.h>
#include <T3d/T3dColor.h>

#include <O3d/O3dObjPrim.h>

#include <Sprite3d/ObjOdron.h>
#include <Sprite3d/World.h>
#include <Sprite3d/Random.h>

#include <SceneArtifice.h>
#include <ShipsMaker.h>
#include <U3d/BonusMaker.h>
#include <U3d/ObjPrimPart.h>


#include <iostream>

//*****************************************
SceneArtifice::SceneArtifice( int pHardness,  Double3& lPos, 
								float pInterval, float  pH, float pV )
:Sprite3dObj(  new ObjOdron( 10, 0)),
 cInterval( pInterval ),
 cH( pH ),
 cV( pV ), 
 cHardness( pHardness ) 
{

  //===== Initialisation des props du generateur =====
  Float4 mat1(   0.8, 0.4, 0.5, 1.0 );

  caPropsGen = new O3dObjProps;
  caPropsGen->ObjPropsFloat4::set( MATERIAL, mat1 );
 
  setObjProps( caPropsGen );

  //=============================================


  //======= Positionnement spacial du Generateur ====
	//	Double3 lPos( 0, -20, 0 );
  getTransf().TransfDouble3::set( POS, lPos );
  SpriteFloat::set( SPRITE_TIMER1, WorldControler::GetTime());

  Double3 spin( 5.0, 8.0, 10.0 );
  SpriteDouble3::set( SPRITE_SPIN, spin );

  cInteractDef = cInteractAtt = 0;
  
}
//------------------------
SceneArtifice::~SceneArtifice()
{
}
//------------------------
GLboolean SceneArtifice::animate()
{
  Sprite3d::animate();

	
  static GLboolean	sFirst=GL_TRUE;


	if( ( WorldControler::GetTime()-SpriteFloat::get( SPRITE_TIMER1)) < cInterval+randp(cInterval) )
		return GL_FALSE;

	 
  SpriteFloat::set( SPRITE_TIMER1,  WorldControler::GetTime());



	ObjVect *lVect = new ObjVect;
	Sprite3dObj* lSp = new Sprite3dObj( lVect );
	WorldControler::Add( lSp );
	
	Float4 color( randp(0.5)+0.5, randp(0.5)+0.5, randp(0.5)+0.5, randp(0.3)+0.6 );
	//  float p = randp( 10 );

	O3dObjProps *lProps = new O3dObjProps();
	lProps->ObjPropsFloat4::set( MATERIAL, color);
	lProps->ObjPropsFloat4::set( EMISSION, color);
	lSp->setObjProps( lProps );


	Double3 lSpeed( randf(30), randf(10)+60, randf(30));
	lSp->SpriteDouble3::set( SPRITE_SPEED,  lSpeed); 

	Double3 lDelta( 0, -1, 0 ); // la gravitation !!!
	lSp->SpriteDouble3::set( SPRITE_DELTA_V, lDelta); 

	
	lSp->SpriteFloat::set(SPRITE_LIFETIME, 4+randf(2));
	
	Double3 lPos;
  lPos[0] =  randf(100);
	//	std::cout << lPos[0] << std::endl;
	lPos[1] = -100;
  lPos[ 2 ] = 0;

  lSp->getTransf().TransfDouble3::set( POS, lPos );


	lSp->setAction( SPRITE_ACTION_KILL, this );
	
	//		Double3 lSpeed2( 0, -20, 0);
	lSpeed.inverse();
	lSpeed *=2;





	ObjPart *lTraine = new ObjPart( 1 );

	Double3 pos(5, 3, 3);
	lTraine->ObjPartDouble3::set( PART_POSITION_RAND, pos );
	lTraine->ObjPartDouble3::set( PART_SPEED, lSpeed );
	Double3 speed2( 0.5, 0.5, 0.5 );
	lTraine->ObjPartDouble3::set( PART_SPEED_RAND, speed2 );
	
	lTraine->ObjPartDouble3::set( PART_ACCELERATION, lDelta);
	
	lTraine->ObjPartFloat::set( PART_SIZE_RAND, 1);
	lTraine->ObjPartFloat::set( PART_GROW, -2);
	lTraine->ObjPartFloat::set( PART_GROW_RAND, -1 );
	lTraine->ObjPartInt::set( PART_GEN_NUMBER, 80 );

	Float4 color1( randp(0.5)+0.5, randp(0.5)+0.5, randp(0.5)+0.5, randp(0.3)+0.6 );
	Float4 color2( -0.1, -0.1, 0.15, -0.3);
	Float4 color3( 0.3, 0.3, 0.1, 0.2 );
	lTraine->ObjPartFloat4::set( PART_COLOR, color1 );
	lTraine->ObjPartFloat4::set( PART_COLOR_CHG, color2 );
 	lTraine->ObjPartFloat4::set( PART_COLOR_RAND, color3 );
	
	lTraine->init( 1, 5.0, 6);
	lTraine->getTransf().TransfDouble3::get( POS )[ 0 ] = -7;



		lVect->add( lTraine);			
		lVect->add(	new ObjOdron( 2, 0)	);
	
	//	lSp->O3dObjDouble3::set( SPRITE_POS,    lPos); 
	
	/*  if( lSp ) 
	{
	  Double3 lD3 ( -15, 0, 0);
	  lSp->SpriteDouble3::set( SPRITE_SPEED, lD3); 
	  lSp->getTransf().TransfDouble3::set( POS, pos );

      WorldControler::Add( lSp  );
	}
	*/
  return GL_FALSE;
}
//------------------------
GLboolean SceneArtifice::leaveWorld()
{
	return GL_FALSE; 
}
//------------------------
GLboolean SceneArtifice::kill( Sprite3d &pMySprite, void*pParam )
{
	ObjVect *lVect = new ObjVect;
	Sprite3dObj* lSp = new Sprite3dObj( lVect ); 

	Float4 lColor = pMySprite.getObjProps()->ObjPropsFloat4::get( MATERIAL );

	Double3 lSpeed2( randf(6)+10, randp(6)+10, randf(6)+10);
	Double3 lDelta2( 0, -1, 0 );
	ObjEtincelles* lEtincel = new ObjEtincelles( lColor,  lSpeed2, lDelta2, rand()%200+300, randp(1)+3 );

	lEtincel->ObjPartInt::set( PART_GEN_VAR_NUMBER, rand()%3+5 );
	lVect->add( lEtincel );			

	lSp->setTransf( pMySprite.getTransf() );
	lSp->SpriteFloat::set(SPRITE_LIFETIME, 3+randf(2) );

	Double3 lSpeed = pMySprite.SpriteDouble3::get( SPRITE_SPEED ); 
	lSp->SpriteDouble3::set( SPRITE_SPEED,  lSpeed); 

	Double3 lDelta = pMySprite.SpriteDouble3::get( SPRITE_DELTA_V );
	lSp->SpriteDouble3::set( SPRITE_DELTA_V, lDelta); 

 
	WorldControler::Add( lSp );
 

	return GL_TRUE;
}

//**************************************

